%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                         %
%                              Save Figures                               %
%                                                                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --------------------------- Description --------------------------------- 

 % This script saves all the main figures of the Article, Appendix and
 % Supplementary Material as CSV files. For it to run properly, make sure
 % (i) to run the main code file before and (ii) that the script
 % FiguresAppendix is saved in the same folder.

% -------------------------------------------------------------------------

% Run the script that prepares the Figures in the Appendix
FiguresAppendix;

% ---------------------------Change director----------------------------------------------

cd ..

cd ProcessedData

%-------------------------- Figure 4 --------------------------------------

Figure_4a = array2table([avec', Esales']);
Figure_4a.Properties.VariableNames(1:2) = {'FirmAge _Years_','LogAverageSales _DifferenceToYear0_'};
writetable(Figure_4a,'Figure_4a.csv')

Figure_4b = array2table([avec', (Emarkup' - Emarkup(1))*100]);
Figure_4b.Properties.VariableNames(1:2) = {'FirmAge _Years_','MarkupGrowth _DifferenceToYear0_'};
writetable(Figure_4b,'Figure_4b.csv')

%-------------------------- Figure 5 --------------------------------------

Figure_5a = array2table(empshare');
Figure_5a.Properties.VariableNames(1:1) = {'EmploymentShare'};
writetable(Figure_5a,'Figure_5a.csv')

Figure_5b = array2table([avec', 100*Exit_age']);
Figure_5b.Properties.VariableNames(1:2) = {'FirmAge _Years_','ExitRate _asPercentage_'};
writetable(Figure_5b,'Figure_5b.csv')

%-------------------------- Figure 6 --------------------------------------

Figure_6a = array2table([t_plot', eta_plot'*100]);
Figure_6a.Properties.VariableNames(1:2) = {'Year','PopulationGrowthRate _asPercentage_'};
writetable(Figure_6a,'Figure_6a.csv')

Figure_6b = array2table([t_plot', Entry_plot'*100, ones(length(t_plot), 1)*EntryRate_counter*100]);
Figure_6b.Properties.VariableNames(1:3) = {'Year','EntryRate _asPercentage_', 'CounterfactualEntry'};
writetable(Figure_6b,'Figure_6b.csv')

Figure_6c = array2table([t_plot', Exit_plot'*100, ones(length(t_plot), 1)*ExitRate_counter*100]);
Figure_6c.Properties.VariableNames(1:3) = {'Year','ExitRate _asPercentage_', 'CounterfactualExit'};
writetable(Figure_6c,'Figure_6c.csv')

Figure_6d = array2table([t_plot', Size_plot', ones(length(t_plot), 1)*AvFirmSize_counter]);
Figure_6d.Properties.VariableNames(1:3) = {'Year','AverageFirmSize _asEmployees_', 'CounterfactualSize'};
writetable(Figure_6d,'Figure_6d.csv')

%-------------------------- Figure 7 --------------------------------------

Figure_7a = array2table([t_plot', plot10y'*100, ones(length(t_plot), 1)*Firms_10y_counter*100]);
Figure_7a.Properties.VariableNames(1:3) = {'Year','ShareFirms+10Years', 'CounterfactualShare+10Years'};
writetable(Figure_7a,'Figure_7a.csv')

Figure_7b = array2table([t_plot', Prods2_plot'*100, Prods5_plot'*100]);
Figure_7b.Properties.VariableNames(1:3) = {'Year','ShareFirms2+Prods', 'ShareFirms5+Prods'};
writetable(Figure_7b,'Figure_7b.csv')

%-------------------------- Figure 8 -------------------------------------

Figure_8a = array2table([t_plot', avgmu_plot', ones(length(t_plot), 1)*CostMarkup_counter]);
Figure_8a.Properties.VariableNames(1:3) = {'Year','CostWeightedMarkup', 'CounterfactualMarkup'};
writetable(Figure_8a,'Figure_8a.csv')

Figure_8b = array2table([avec', CostMarkup_A', CostMarkup_A_counter']);
Figure_8b.Properties.VariableNames(1:3) = {'Firm Age _Years_','2% Population Growth', '0.24% Population Growth'};
writetable(Figure_8b,'Figure_8b.csv')

%-------------------------- Figure 9 -------------------------------------

Figure_9 = array2table([t_plot', gQ_term'*100, gN_term'*100, g_plot'*100]);
Figure_9.Properties.VariableNames(1:4) = {'Year','EfficiencyGrowthTerm', 'VarietyGrowthTerm', 'ProductivityGrowthRate'};
writetable(Figure_9,'Figure_9.csv')

%--------------------- Figure 3 - Appendix --------------------------------

Figure_App_2a = array2table([avec', (Exit_age*100)', (Exit_age_counter*100)']);
Figure_App_2a.Properties.VariableNames(1:3) = {'FirmAge _inYears_','Exit - 2% Population Growth','Exit - 0.24% Population Growth'};
writetable(Figure_App_2a,'Figure_App_2a.csv')

Figure_App_2b = array2table([avec', (Esales)', (Esales_counter)']);
Figure_App_2b.Properties.VariableNames(1:3) = {'FirmAge _inYears_','LogSales - 2% Population Growth','LogSales - 0.24% Population Growth'};
writetable(Figure_App_4b,'Figure_App_2b.csv')

%--------------------- Figure 4 - Appendix --------------------------------

Figure_App_3 = array2table([avec', FirmAge_dens(avec)', FirmAge_dens_counter(avec)']);
Figure_App_3.Properties.VariableNames(1:3) = {'FirmAge _inYears_','Age Density - 2% Population Growth','Age Density - 0.24% Population Growth'};
writetable(Figure_App_3,'Figure_App_3.csv')

%---------------- Figure 2 - Supplementary Material -----------------------

Figure_SM_1a = array2table([avec', ExpcAge_An(1, :)', ExpcAge_An(2, :)', ExpcAge_An(3, :)', ExpcAge_An(4, :)', ExpcAge_An(5, :)', ExpcAge_A']);
Figure_SM_1a.Properties.VariableNames(1:7) = {'FirmAge _inYears_','OneProduct','TwoProducts','ThreeProducts','FourProducts','FiveProducts','Average Across Firms'};
writetable(Figure_SM_1a,'Figure_SM_1a.csv')

Figure_SM_1b = array2table([avec', CondProbs(1, :)', CondProbs(2, :)', CondProbs(3, :)', CondProbs(4, :)', CondProbs(5, :)']);
Figure_SM_1b.Properties.VariableNames(1:6) = {'FirmAge _inYears_','OneProduct','TwoProducts','ThreeProducts','FourProducts','FiveProducts'};
writetable(Figure_SM_1b,'Figure_SM_1b.csv')

cd ..

cd Code